# -*- coding: utf-8 -*-
from . import BaseController
from backend.libs import myjson as json
from backend.models import Producer, Team, User, Element
from backend.libs.args_util import PageArgs
from datetime import datetime


class ListController(BaseController):

    def get(self):
        args = PageArgs(self, 'get', 'name')
        p = Producer.objects
        team_id = self.int_argument('team_id', -1)
        if team_id > 0:
            p = p.filter(team_id=team_id)
        p = p.select_related('user', 'team').order_by('-create_time')
        paginator = self.pagination(p.all(), number_per_page=args.pageSize, page_num=args.pageNum)
        return self.render_json(data=paginator)


class DetailController(BaseController):

    def get(self):
        id = self.int_argument('id', -1)
        if id <= 0:
            return self.render_json(code=1001, msg='参数错误')
        producer = Producer.objects.filter(id=id).first()
        return self.render_json(data=producer.to_dict())


class BaseProducerController(BaseController):

    def save_producer(self, producer, data, user, type=0):
        producer.school = data.get('school', '')
        producer.honer = data.get('honer', '')
        producer.experience = data.get('experience', '')
        producer.desc = data.get('desc', '')
        producer.level = data.get('level', 0)
        tmp_score = data.get('score', 0)
        if tmp_score:
            producer.score = tmp_score
        tmp_score = data.get('score_total', 0)
        if tmp_score:
            producer.score_total = tmp_score
        tmp_score_num = data.get('score_num', 0)
        if tmp_score_num:
            producer.score_num = tmp_score_num
        
        producer.user = user
        team_id = data.get('team_id')
        team = None
        if team_id and int(team_id) > 0:
            team = Team.objects.filter(id=team_id).first()
        producer.team = team
        if type == 0:
            producer.create_time = datetime.now()
        producer.update_time = datetime.now()
        producer.save()

        if type == 1:
            for e in producer.elements.all():
                producer.elements.remove(e)
        tmp_elements = data.get('elements')
        for e in Element.objects.filter(id__in=tmp_elements).all():
            producer.elements.add(e)
        return producer


class CreateController(BaseProducerController):

    def post(self):
        data = json.loads(self.request.body)
        print(data)
        user_id = data.get('user_id', '')
        if not user_id or int(user_id) <= 0:
            return self.render_json(code=1001, msg='参数错误')
        owner_id = data.get('user_id')
        user = None
        if owner_id and int(owner_id) > 0:
            user = User.objects.filter(id=owner_id).first()
        if not user:
            return self.render_json(code=1001, msg='用户不存在')
        producer = Producer()
        producer = self.save_producer(producer, data, user)
        return self.render_json(data=producer.to_dict())


class UpdateController(BaseProducerController):

    def post(self):
        data = json.loads(self.request.body)
        id = data.get('id')
        user_id = data.get('user_id', '')
        if not user_id or int(user_id) <= 0:
            return self.render_json(code=1001, msg='参数错误')
        owner_id = data.get('user_id')
        user = None
        if owner_id and int(owner_id) > 0:
            user = User.objects.filter(id=owner_id).first()
        if not user:
            return self.render_json(code=1001, msg='用户不存在')
        producer = Producer.objects.filter(id=id).first()
        if not producer:
            return self.render_json(code=1001, msg='生产不存在')
        producer = self.save_producer(producer, data, user, type=1)
        return self.render_json(data=producer.to_dict())


class DeleteController(BaseController):

    def post(self, producer_id):
        producer_id = int(producer_id)
        if producer_id > 0:
            producer = Producer.objects.filter(id=producer_id).first()
            if producer:
                type = self.get_argument('type', '')
                if type == 'removeFromTeam':
                    producer.team_id = None
                    producer.save()
                else:
                    producer.delete()

        return self.render_json()
